﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.Text;
using System.Threading.Tasks;
using SpectrumAnalyser.ModelObjects.Enum;

namespace SpectrumAnalyser.Common.Hosting
{
    public static class WcfHelper
    {
        public static Binding GetBinding(UriScheme uriScheme)
        {
            switch (uriScheme)
            {
                case UriScheme.Tcp:
                    var binding = new NetTcpBinding(SecurityMode.Transport)
                        {

                            MaxReceivedMessageSize = int.MaxValue,
                            MaxConnections = 1000,
                            ListenBacklog = 1000,
                            PortSharingEnabled = true,

                            TransferMode = TransferMode.Streamed
                        };

#if DEBUG
                    binding.CloseTimeout = new TimeSpan(int.MaxValue);
                    binding.SendTimeout = new TimeSpan(int.MaxValue);
                    binding.ReceiveTimeout = new TimeSpan(int.MaxValue);
                    binding.OpenTimeout = new TimeSpan(int.MaxValue);
#endif

                    return binding;
                case UriScheme.WsHttp:
                    var wsHttpBinding = new WSHttpBinding(SecurityMode.Transport)
                        {
                            Name = "WSHttpBinding1",
                            MaxReceivedMessageSize = int.MaxValue,
                            MaxBufferPoolSize = int.MaxValue,
                        };

#if DEBUG
                    wsHttpBinding.CloseTimeout = new TimeSpan(int.MaxValue);
                    wsHttpBinding.SendTimeout = new TimeSpan(int.MaxValue);
                    wsHttpBinding.ReceiveTimeout = new TimeSpan(int.MaxValue);
                    wsHttpBinding.OpenTimeout = new TimeSpan(int.MaxValue);
#endif

                    return wsHttpBinding;
                case UriScheme.Http:
                    var httpBinding = new BasicHttpBinding()
                        {
                            Name = "HttpBinding1",
                            MaxReceivedMessageSize = int.MaxValue,
                            MaxBufferPoolSize = int.MaxValue
                        };

#if DEBUG
                    httpBinding.CloseTimeout = new TimeSpan(int.MaxValue);
                    httpBinding.SendTimeout = new TimeSpan(int.MaxValue);
                    httpBinding.ReceiveTimeout = new TimeSpan(int.MaxValue);
                    httpBinding.OpenTimeout = new TimeSpan(int.MaxValue);
#endif

                    return httpBinding;
            }
            throw new Exception();
        }

        public static Uri GetEndpointUri(UriScheme scheme, Type type)
        {
            return GetEndpointUri(scheme, type.ToString());
        }

        public static Uri GetEndpointUri(UriScheme scheme, string type)
        {
            string schemeString = string.Empty;
            switch (scheme)
            {
                case UriScheme.Http:
                    schemeString = "http";
                    break;
                case UriScheme.WsHttp:
                    schemeString = "https";
                    break;
                case UriScheme.Tcp:
                    schemeString = "net.tcp";
                    break;
            }
            var machineName = Dns.GetHostEntry(Dns.GetHostName()).HostName.ToLower();
            return new Uri(string.Format("{0}://{1}/{2}", schemeString, machineName, type));
        }
    }
}
